package com.cdtu.store.item.service.impl;

import com.cdtu.store.common.entity.CategoryBrandEntity;
import com.cdtu.store.common.entity.CategoryEntity;
import com.cdtu.store.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.store.common.utils.PageUtils;
import com.cdtu.store.common.utils.Query;

import com.cdtu.store.item.dao.CategoryDao;
import com.cdtu.store.item.service.CategoryService;
import org.springframework.util.StringUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryBrandService categoryBrandService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        List<CategoryEntity> levelOne = entities.stream().filter(categoryEntity ->
             categoryEntity.getParentId() == 0
        ).map(meun->{
            meun.setChildren(getChildrens(meun,entities));
            return meun;
        }).sorted((menu1,menu2)->{
            return menu1.getSort()-menu2.getSort();
        }).collect(Collectors.toList());

        return levelOne;
    }
    //递归查找
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(categoryEntity ->
                categoryEntity.getParentId().equals(root.getId())
        ).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> menu1.getSort() - menu2.getSort()).collect(Collectors.toList());
        return children;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO 检查当前删除的菜单是否被别的地方引用

        //逻辑删除
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public void updateCasecade(CategoryEntity category) {
        CategoryBrandEntity categoryBrandEntity = new CategoryBrandEntity();
        categoryBrandEntity.setCategoryId(category.getId());
        categoryBrandEntity.setCategoryName(category.getName());
        this.updateById(category);
        if(!StringUtils.isEmpty(category.getName())){
            categoryBrandService.updateCategory(category.getId(),category.getName());
            //TODO 更新其他表关于category的冗余信息
        }
    }

    @Override
    public List<String> queryNamesByIds(List<Long> ids) {
        List<String> list = new ArrayList<>();
        List<CategoryEntity> categoryEntities = this.listByIds(ids);
        categoryEntities.forEach(category ->{
            list.add(category.getName());
        });
        return list;
    }

    @Override
    public List<CategoryEntity> queryAllByCid3(Long id) {
        CategoryEntity c3 = this.getById(id);
        CategoryEntity c2 = this.getById(c3.getParentId());
        CategoryEntity c1 = this.getById(c2.getParentId());
        return Arrays.asList(c1,c2,c3);
    }

}